package com.jinlejia.erp.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityEntering1Binding;
import com.jinlejia.erp.utils.ActivityCollector;
import com.jinlejia.erp.utils.DateUtil;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.weight.CustomToast;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.Map;

/**
 * 录带看第一步（activity）
 */

public class EnteringVisitActivity1 extends AppCompatActivity implements OnDateSetListener {

    private String time = "today";
    private long startMillTime=0, endMillTime=0;
    private String csCode,type,name,sex;
    private Map<String, Object> yuekanMap;

    private ActivityEntering1Binding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_entering1);
        binding.setEvent(this);
        initView();
        EventBus.getDefault().registerSticky(this);
        ActivityCollector.addActivity2(this);
        csCode = getIntent().getStringExtra("csCode");
        type =  getIntent().getStringExtra("type");
        name = getIntent().getStringExtra("name");
        sex = getIntent().getStringExtra("sex");
    }

    private void initView() {
        binding.rgMain.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.yesterday_radio) {
                time = "yesterday";
            } else {
                time = "today";
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.start_time:       //开始时间
                TimePickerDialog startPicker = new TimePickerDialog.Builder()
                        .setType(Type.HOURS_MINS)
                        .setTitleStringId("开始时间")
                        .setCurrentMillseconds(System.currentTimeMillis())
                        .setThemeColor(getResources().getColor(R.color.yellow))
                        .setWheelItemTextNormalColor(getResources().getColor(R.color.hintColor))
                        .setWheelItemTextSelectorColor(getResources().getColor(R.color.yellow))
                        .setCallBack(this)
                        .build();
                startPicker.show(getSupportFragmentManager(), "hours_minus");
                break;
            case R.id.end_time:         //结束时间
                TimePickerDialog endPicker = new TimePickerDialog.Builder()
                        .setType(Type.HOURS_MINS)
                        .setTitleStringId("结束时间")
                        .setCurrentMillseconds(System.currentTimeMillis())
                        .setThemeColor(getResources().getColor(R.color.yellow))
                        .setWheelItemTextNormalColor(getResources().getColor(R.color.hintColor))
                        .setWheelItemTextSelectorColor(getResources().getColor(R.color.yellow))
                        .setCallBack(this)
                        .build();
                endPicker.show(getSupportFragmentManager(), "hours_minus");
                break;
            case R.id.bt_next:          //下一步
                Intent intent = new Intent(this, EnteringVisitActivity2.class);
                if (TextUtils.isEmpty(binding.startTimeText.getText().toString())) {
                    CustomToast.showToast(EnteringVisitActivity1.this, "请输入开始时间", 1500);
                } else if (TextUtils.isEmpty(binding.endTimeText.getText().toString())) {
                    CustomToast.showToast(EnteringVisitActivity1.this, "请输入结束时间", 1500);
                } else {
                    if( null != yuekanMap ){
                        EventBus.getDefault().postSticky(yuekanMap,"TAG_YUEKAN_HOUSE_LIST");
                    }
                    intent.putExtra("startTime", binding.startTimeText.getText());
                    intent.putExtra("endTime", binding.endTimeText.getText());
                    intent.putExtra("csCode", csCode);
                    intent.putExtra("type",type);
                    intent.putExtra("name", name);
                    intent.putExtra("sex", sex);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        if (timePickerView.getTitle().equals("开始时间")) {
            startMillTime = millseconds;
            if ( endMillTime != 0 && (startMillTime >= endMillTime)) {
                CustomToast.showToast(EnteringVisitActivity1.this, "结束时间必须大于开始时间", 1500);
            } else {
                if (time.equals("today")) {
                    binding.startTimeText.setText(DateUtil.getTime(System.currentTimeMillis()) + " " + DateUtil.getTime(millseconds, "HH:mm"));
                } else {
                    binding.startTimeText.setText(DateUtil.getTime(System.currentTimeMillis() - 24 * 60 * 60 * 1000) + " " + DateUtil.getTime(millseconds, "HH:mm"));
                }
            }

        } else if (timePickerView.getTitle().equals("结束时间")) {
            endMillTime = millseconds;
            if ( startMillTime != 0 && (startMillTime >= endMillTime)) {
                CustomToast.showToast(EnteringVisitActivity1.this, "结束时间必须大于开始时间", 1500);
            } else {
                if (time.equals("today")) {
                    binding.endTimeText.setText(DateUtil.getTime(System.currentTimeMillis()) + " " + DateUtil.getTime(millseconds, "HH:mm"));
                } else {
                    binding.endTimeText.setText(DateUtil.getTime(System.currentTimeMillis() - 24 * 60 * 60 * 1000) + " " + DateUtil.getTime(millseconds, "HH:mm"));
                }
            }
        }
    }
    @Subscriber(tag = "TAG_YUEKAN_INFO")
    private void yueKanInfo(  Map<String, Object> map ){
        EventBus.getDefault().removeStickyEvent(map.getClass(),"TAG_YUEKAN_INFO");
        binding.startTimeText.setText( DateUtil.getTime(map.get("appointmentStartTime").toString(),"yyyy-MM-dd HH:mm") );
        binding.endTimeText.setText( DateUtil.getTime(map.get("appointmentEndTime").toString(),"yyyy-MM-dd HH:mm") );
        csCode = map.get("csCode").toString();
        if( map.get("type").toString().equals("1") ) {
            type = "sale";
        }else{
            type = "rent";
        }
        name = map.get("customerName").toString();
        yuekanMap = map;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        ActivityCollector.removeActivity2(this);
        super.onDestroy();
    }
}
